﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace ERPForms
{
    public partial class SemnaturiFrm : FrmRezumat
    {
        private Boolean _Select = false;
        public String selectat = String.Empty;
        public Boolean SelectSemnaturi
        {
            get { return _Select; }
            set { _Select = value; }
        }

        public SemnaturiFrm(ModuleClass mc):base(mc)
        {
            InitializeComponent();
            workConnString = mc.workConnectionString;
            eSRPDataSet.Semnaturi.ColoanaColumn.DefaultValue = 0;
            eSRPDataSet.Semnaturi.SystemColumn.DefaultValue = false;
            eSRPDataSet.Semnaturi.LaComandaColumn.DefaultValue = true;
            eSRPDataSet.Semnaturi.LaReceptieColumn.DefaultValue = true;
        }

        private void SemnaturiFrm_Load(object sender, EventArgs e)
        {
            AddBindingSource(semnaturiBindingSource);
            listTableAdaptor.Add(semnaturiTableAdapter.Adapter);


            SqlConnection con = DeschideConexiune(workConnString);
            this.semnaturiTableAdapter.Connection = con;

            this.semnaturiTableAdapter.Fill(this.eSRPDataSet.Semnaturi);

            if (_Select)
            {
                String seSel = GetSetariLocale(con, "SMSL", "", ModuleInfo.CodUnitate);
                if (seSel != String.Empty)
                {
                    foreach (DataGridViewRow dv in semnaturiDGV.Rows)
                    {
                        if (dv.Cells[IDSemnaturaColumn.Index].Value != null)
                        {
                            if (seSel.Contains(dv.Cells[IDSemnaturaColumn.Index].Value.ToString() + ","))
                                dv.Cells[SelectatColumn.Index].Value = true;
                            else
                                dv.Cells[SelectatColumn.Index].Value = false ;
                        }
                    }
                }
            }
            InchideConexiune(con);
            HandleSalvare += Salvare;
            
            SelectatColumn.Visible = _Select;
            IesireTSB.Text = "Continua";
            AddButton("Liste", global::ERPStudio.Properties.Resources._001_07, SemnaturiListe, ToolStripItemAlignment.Right, false);

            if (ModuleInfo.ModuleName != "Fares")
            {
                LaComandaColumn.Visible = false;
                LaReceptieColumn.Visible = false;
                Width -= 200;
                IesireTSB.Text = "Iesire";
            }
            
        }

        private void SemnaturiListe(object sender, EventArgs e)
        {
            new ListeSemnaturiFrm(ModuleInfo).ShowDialog();
        }

        public override void FrmRezumat_FormClosing(object sender, FormClosingEventArgs e)
        {
            selectat = String.Empty;
            foreach (DataGridViewRow dv in semnaturiDGV.Rows)
            {
                if (Convert.ToBoolean(dv.Cells[SelectatColumn.Index].Value) == true)
                {
                    if (selectat != String.Empty)
                        selectat += ",";
                    selectat += dv.Cells[IDSemnaturaColumn.Index].Value.ToString();

                }
            }
            base.FrmRezumat_FormClosing(sender, e);
        
        }

        override public void Salvare(object sender, EventArgs args)
        {
            base.Salvare(this, args);
            if (EroareSalvare)
                return;
            selectat = String.Empty;
            foreach (DataGridViewRow dv in semnaturiDGV.Rows)
            {
                if (Convert.ToBoolean(dv.Cells[SelectatColumn.Index].Value) == true)
                {
                    if (selectat != String.Empty)
                        selectat += ",";
                    selectat += dv.Cells[IDSemnaturaColumn.Index].Value.ToString();

                }
            }
            SqlConnection con = DeschideConexiune(workConnString);
            SetSetariLocale(con, "SMSL", selectat + ",", ModuleInfo.CodUnitate);
            InchideConexiune(con);
        }
    }
}
